/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.bean.questionnaire;

import org.ipti.br.tag.bean.AppBaseBean;
import org.ipti.br.tag.bean.SessionBean;
import org.ipti.br.tag.bean.dataManipulation.SelectionElement;
import org.ipti.br.tag.bo.CampaignBO;
import org.ipti.br.tag.bo.HbBO;
import org.ipti.br.tag.bo.UnityBO;
import org.ipti.br.tag.bo.HumanBO;
import org.ipti.br.tag.entity.Campaign;
import org.ipti.br.tag.entity.Human;
import org.ipti.br.tag.entity.Unity;
import org.ipti.br.tag.entity.enums.CampaignType;
import java.io.*;
import java.util.List;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;

@Named @ViewScoped
public class ConsentTermBean extends AppBaseBean implements Serializable {
    
    @Inject private SessionBean sessionBean;
    @Inject private HbBO hbBO;
    @Inject private UnityBO unityBO;
    @Inject private CampaignBO campaignBO;

    private List<SelectionElement<Human, Boolean>> aceptation;
    private Campaign campaign;
    private Unity unity;
    
    private Integer year = getActualYear();

    public void saveHB() {
        try {
            List<String> measured = hbBO.saveConsentTerm(aceptation, campaign);
            if (measured.isEmpty()) {
                addInfoMessage(":fillConsentForm:message", "Sucesso", "Salvo com sucesso no servidor");
            } else {
                for (String string : measured) {
                    addErrorMessage(":fillConsentForm:message", "Meteram a agulha em alguém sem permissão? D:",
                            string + " possui medida do HB1 diferente de 0");
                }
                addInfoMessage(":fillConsentForm:message", "As demais informações foram salvas", "Nem tudo está perdido (:");
            }
        } catch(Exception e) {
            e.printStackTrace();
            addErrorMessage(":fillConsentForm:message", "Erro ao salvar os dados no servidor", ":(");
        }
    }

    public void print() {
        boolean sucess = hbBO.printConsentTerm(aceptation, campaign, unity, sessionBean.getLoggedActor().getUnity());
        if (sucess) {
            addInfoMessage(":createConsentForm:message", "Sucesso!", "Os relatórios gerados estão lindos, né? :D");
        } else {
            addErrorMessage(":createConsentForm:message", "Ocorreu algum problema ao gerar os relatórios", ":(");
        }
    }
    
    public void loadDataTable() {
        if (campaign != null && unity != null) {
            this.aceptation = hbBO.getAuthorizations(campaign, unity);
        }
    }
    
    public List<Unity> getUnityList() {
        List<Unity> list = unityBO.getClassLessonList(sessionBean.getLoggedActor().getUnity(),year);
        return list;
    }
   
    public List<Campaign> getCampaignList() {
        return campaignBO.findAllCampaign(CampaignType.ANEMIA_FERROPRIVA);
    }

    public List<SelectionElement<Human, Boolean>> getAceptation() {
        return aceptation;
    }

    public void setAceptation(List<SelectionElement<Human, Boolean>> aceptation) {
        this.aceptation = aceptation;
    }

    public Campaign getCampaign() {
        return campaign;
    }

    public void setCampaign(Campaign campaign) {
        this.campaign = campaign;
    }

    public Unity getUnity() {
        return unity;
    }

    public void setUnity(Unity unity) {
        this.unity = unity;
    }

    public Integer getYear() {
        return year;
    }

    public void setYear(Integer year) {
        this.year = year;
    }

}
